<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/14
 * Time: 16:50
 */

namespace Admin\Controller;
use User\Api\UserApi;


class CheckController extends AdminController
{
    public function index(){
        $uid=session('user_auth.uid');
        //审核权限
        if($uid!=1){
            $grout_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
        }
        $listsCount = M('shop')->count();
        $Page       = new \Think\Page($listsCount,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $lists = M('shop')->order(' check_status asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($lists as $ke=>$ve){

        }
        $this->assign('list',$lists);
        $this->assign('_page',$show);
        $this->display();
    }

    public function check_status($check_status=null,$id){
        if($id && $check_status){
            $status['check_status']=$check_status;
            if(is_array($id)){
                foreach ($id as $k=>$v){
                    $check=M('shop')->where(array('id'=>$v))->find();
                    if($check['check_status']==1){
                        $res=M('shop')->where(array('id'=>$v))->save($status);
                        $this->auto_user($v);
                    }
                }
            }else{
                $check=M('shop')->where(array('id'=>$id))->find();
                if($check['check_status']==1){
                    $res=M('shop')->where(array('id'=>$id))->save($status);
                    $this->auto_user($id);
                }
            }
            if($res){
                $this->success('审核成功');
            }else{
                $this->error('请勿重复审核');
            }
        }else{
            $this->error('审核失败,请选择审核内容');
        }
    }
    public function auto_user($id){
        $check=M('shop')->where(array('id'=>$id))->find();
        $username=$check['phone'];
        $password='123456';
        $email=$check['phone'].'@qq.com';
        /* 调用注册接口注册用户 */
        $User   =   new UserApi;
        $uid    =   $User->register($username, $password, $email);
        if(0 < $uid){ //注册成功
            $user = array('uid' => $uid, 'nickname' => $username, 'status' => 1,'icon' => '',
                'pid' => '','flag' => '','sort' => '','content' => '','shop_id'=>$id);
            $Mid=M('Member')->add($user);
            if(!$Mid){
                $this->error('审核失败！');
            } else {
                M('auth_group_access')->add(array('uid'=>$Mid,'group_id'=>'1'));
            }
        } else { //注册失败，显示错误信息
            $this->error($this->showRegError($uid));
        }
    }
    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }
}